﻿/*******************************************************************************
 * Copyright © 2016 NFine.Framework 版权所有
 * Author: NFine
 * Description: NFine快速开发平台
 * Website：http://www.nfine.cn
*********************************************************************************/

using NFine.Application._02_Weixin;
using NFine.Application.SystemManage;
using NFine.Code;
using NFine.Domain.Entity.SystemManage;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using NFine.Application.Biz;
using NFine.Application.InformationCenter;
using NFine.Domain._02_ViewModel;

namespace NFine.Web.Controllers
{
    [HandlerLogin]
    public class HomeController : ControllerBase
    {
        private UserApp userApp = new UserApp();
        private UserOrderApp userOrderApp = new UserOrderApp();
        private UserDetailApp userDetailApp = new UserDetailApp();
        private SysNewsApp sysNewsApp = new SysNewsApp();
        private SysMessagesApp messagesApp = new SysMessagesApp();
        private UserWithdrawalsApp userWithdrawalsApp = new UserWithdrawalsApp();

        //}
        [HttpGet]
        public ActionResult Default()
        {
            return View();
        }

        [HttpGet]
        public ActionResult MemberDefault()
        {
            return View();
        }

        [HttpGet]
        public ActionResult About()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(Ignore = false)]
        public override ActionResult Index()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            if (u != null)
            {
                var ud = userDetailApp.GetEntity(u.UserId);
                if(ud!=null)
                    ViewData["level"] = ud.F_UserLevel;
            }
            return View();
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetDataCountForSystem()
        {
            //// 当前会员总量
            //int memberCount = userApp.GetAllMemberCount();
            //// 本月新增会员数量
            //int newMemberCount = userApp.GetCurMonthMemberCount();
            // 本月订单总数量
            int orderCount = userOrderApp.GetCurMonthOrderCount();
            // 本月销售总金额
            decimal monthMoney = userOrderApp.GetCurMonthMoney();
            // 平台销售总金额
            decimal allMoney = userOrderApp.GetAllMoney();
            //提现总金额
            decimal tixianMoney = userWithdrawalsApp.GetAllTixianMoney();



            var data = new
            {
                //memberCount = memberCount,
                //newMemberCount = newMemberCount,
                orderCount = orderCount,
                monthMoney = monthMoney.ToString("C"),
                allMoney = allMoney.ToString("C"),
                tixianMoney = tixianMoney.ToString("C")
            };
            return Content(data.ToJson());
        }

        /// <summary>
        /// 消费统计图表数据
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMoneyChartJson(DateTime startTime, DateTime endTime)
        {
            return Content(userOrderApp.GetChartList(startTime, endTime).ToJson());
        }

        /// <summary>
        /// 会员统计图表数据
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetMemberChartJson(DateTime startTime, DateTime endTime)
        {
            return Content(userApp.GetChartList(startTime, endTime).ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetConsumeTop()
        {
            //userDetailApp.GetConsumeTopNew();
            return Content(userDetailApp.GetConsumeTopNew().ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetTuiJianTop()
        {
            //userDetailApp.GetConsumeTopNew();
            return Content(userDetailApp.GetTuiJianTop().ToJson());
        }
        

       [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetNewsTop()
        {
            return Content(sysNewsApp.GetLatelyNews(0).ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetNoticeTop()
        {
            return Content(sysNewsApp.GetLatelyNews(2).ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetMessageTop()
        {
            var topCount = 22;
            var userId = OperatorProvider.Provider.GetCurrent().UserId;
            return Content(messagesApp.GetMemberUnReadList(topCount, userId).ToJson());
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult GetDataCountForMember()
        {
            var userId = OperatorProvider.Provider.GetCurrent().UserId;
            View_MemberDefaultData memberDefaultData = userDetailApp.GetMemberDefaultData(userId);
            var data = new
            {
                teamCount = memberDefaultData.teamCount,
                teamConsume = memberDefaultData.teamConsume.ToString("C"),
                memberConsume = memberDefaultData.memberConsume.ToString("C"),
                memberBonus = memberDefaultData.memberBonus.ToString("C"),
                memberProperty = memberDefaultData.memberProperty.ToString("C"),
                userlevel= memberDefaultData.level,
                lastMoney=memberDefaultData.memberLast,
                userlevelA= memberDefaultData.levelA
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public ActionResult password()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            ViewData["username"] = u.UserCode;
            return View();
        }

        [HttpGet]
        public ActionResult MemberInfo()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            var model = userApp.GetForm(u.UserId);
            return View(model);
        }

        [HttpPost]
        [HandlerAjaxOnly]
        public ActionResult ChangePassword(string oldPassword,string newPassword)
        {
            // 获取用户信息
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null)
            {
                memberId = u.UserId;
            }
            

            // 密码修改
            int result = userApp.ChangePassword(memberId,oldPassword,newPassword);

            if(result == -1)
                return Error("密码不正确，请重新输入！");

            if (result == 0)
                return Error("获取用户信息失败，请重新登录！");

            
            return Success("密码修改成功！");

        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult UpdateMemberInfo(UserEntity userEntity)
        {
            // 获取用户信息
            var u = OperatorProvider.Provider.GetCurrent();
            string memberId = "";
            if (u != null)
            {
                memberId = u.UserId;
            }

            userEntity.F_Id = memberId;

            if (userApp.IsExistDepartmentId(userEntity.F_DepartmentId, memberId))
                return Error("身份证号：“" + userEntity.F_DepartmentId + "”已注册，请重新输入身份证号！");

            int result = userApp.UpdateMemberInfo(userEntity);
            if(result > 0)
                return Success("资料修改成功！");
            return Error("资料修改失败！");

        }

    }
}